package entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Size;

@Table(name = "Utilisateur")
@Entity
public class Utilisateur implements Serializable {

    private Long id_utilisateur;
    private String mail;
    private String pw;
    private String nom;
    private String prenom;
    private String role;
    private List<Tache> estResponsable;
    private List<Tache> participations;
    private List<Avis> avis;
    private List<Message> messages;
    private List<Document> documents;
    private Date dateCreation;

    public Utilisateur() {
        this.dateCreation = new Date();
        this.participations = new ArrayList<>();
        this.avis = new ArrayList<>();
        this.messages = new ArrayList<>();
        this.documents = new ArrayList<>();
        this.role = "Normal";
    }

    public Utilisateur(String nom, String prenom, String mail, String pw) {
        this.nom = nom;
        this.prenom = prenom;
        this.mail = mail;
        this.pw = pw;
    }

    public Utilisateur(String nom, String prenom, String mail, String pw, String role) {
        this.nom = nom;
        this.prenom = prenom;
        this.mail = mail;
        this.pw = pw;
        this.role = role;
        if (role.equals("Responsable")) {
            this.estResponsable = new ArrayList<>();
        }
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId_utilisateur() {
        return id_utilisateur;
    }

    public void setId_utilisateur(Long id_utilisateur) {
        this.id_utilisateur = id_utilisateur;
    }

    @Column(name = "MAIL", unique = true, nullable = false)
    @Size(min = 6, max = 100, message = "L'identifiant doit faire au moins 6 caractères !")
    public String getMail() {
        return mail;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }

    @Column(name = "PW", unique = false, nullable = false)
    @Size(min = 6, max = 20, message = "Le mot de passe doit faire au moins 6 caractères !")
    public String getPw() {
        return pw;
    }

    public void setPw(String pw) {
        this.pw = pw;
    }

    @Column(name = "NOM", nullable = false)
    @Size(min = 1, max = 30, message = "Nom vide !")
    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    @Column(name = "PRENOM", nullable = false)
    @Size(min = 1, max = 30, message = "Prénom vide !")
    public String getPrenom() {
        return prenom;
    }

    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }

    @Column(name = "ROLE", nullable = false)
    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "responsable")
    public List<Tache> getEstResponsable() {
        return estResponsable;
    }

    public void setEstResponsable(List<Tache> estResponsable) {
        this.estResponsable = estResponsable;
    }

    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinTable(name = "User_x_Tache", joinColumns = {
        @JoinColumn(name = "ID_UTILISATEUR", nullable = false, updatable = false)},
            inverseJoinColumns = {
                @JoinColumn(name = "ID_TACHE",
                        nullable = false, updatable = false)})
    public List<Tache> getParticipations() {
        return participations;
    }

    public void setParticipations(List<Tache> participations) {
        this.participations = participations;
    }

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "auteur")
    public List<Avis> getAvis() {
        return avis;
    }

    public void setAvis(List<Avis> avis) {
        this.avis = avis;
    }

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "auteur")
    public List<Message> getMessages() {
        return messages;
    }

    public void setMessages(List<Message> messages) {
        this.messages = messages;
    }

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "proprietaire")
    public List<Document> getDocuments() {
        return documents;
    }

    public void setDocuments(List<Document> documents) {
        this.documents = documents;
    }

    @Temporal(TemporalType.DATE)
    @Column(name = "DATE", nullable = false, updatable = false)
    public Date getDateCreation() {
        return dateCreation;
    }

    public void setDateCreation(Date dateCreation) {
        this.dateCreation = dateCreation;
    }

}
